<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <div class="flex">
      <el-button size="small" @click="goBack"> 返回 </el-button>
      <el-descriptions style="margin-left: 20px">
        <el-descriptions-item label="用能预算时间">
          {{ info.year }} 年</el-descriptions-item
        >
        <el-descriptions-item label="申报单位数量">{{
          info.organizationCounts
        }}</el-descriptions-item>
        <el-descriptions-item label="申请用能预算经费">
          {{ info.budgetFund }} 元</el-descriptions-item
        >
      </el-descriptions>
      <template v-if="type == 1">
        <el-button
          v-if="info.status == 0"
          v-hasPermi="['energy:budget-summary:audit:1']"
          type="primary"
          size="small"
          @click="submit(1)"
          >上报</el-button
        >
        <el-button
          v-if="info.status == 7"
          v-hasPermi="['energy:budget-summary:audit:4-2']"
          type="primary"
          size="small"
          @click="handleOpinion(4)"
          >通过</el-button
        >
        <el-button
          v-if="info.status == 7"
          v-hasPermi="['energy:budget-summary:audit:5-2']"
          type="danger"
          size="small"
          @click="handleOpinion(5)"
          >退回</el-button
        ></template
      >
    </div>

    <el-table :data="info.budgetDeclareVos" style="width: 100%" class="mt20">
      <el-table-column
        prop="sysDeptName"
        label="申报单位"
        minWidth="150"
        align="center"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="基本信息" align="center">
        <el-table-column
          prop="budgetFund"
          label="申请用能预算经费（元）"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="year"
          label="用能预算利用时间"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="contact"
          label="联系人"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="contactNumber"
          label="联系电话"
          width="120"
          align="center"
        >
        </el-table-column>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in titleList"
        :key="index"
        :label="item.titleName"
        align="center"
      >
        <el-table-column
          v-for="(it, idx) in item.subTitleNames"
          :key="idx"
          :prop="'a' + idx"
          :label="
            item.isUseStandard == 1 && idx == 0
              ? it
              : it + `（${item.energyUnit}）`
          "
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <span
              :class="
                Number(item[`val${scope.$index}`][idx]) > 0 ? 'red' : 'green'
              "
              >{{ item[`val${scope.$index}`][idx] }}</span
            >
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>
    <!-- 反馈对话框 -->
    <el-dialog
      title="审核人意见"
      :visible.sync="show"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="model" :rules="rules" label-width="100px">
        <el-form-item label="意见" prop="content">
          <el-input
            type="textarea"
            v-model="model.content"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="close()">取 消</el-button>
      </div>
    </el-dialog>
  </lb-layout>
</template>

  <script>
import {
  detailDynamicTitleTemplate,
  detail,
  auditSummary,
} from "@/api/work/budgetSummaryList.js";
import { dealElectricity } from "@/utils";
export default {
  name: "summaryList",
  filters: {
    numDealfilters(num) {
      if (num) {
        return dealElectricity(num, 1, 2);
      } else {
        return "";
      }
    },
  },
  data() {
    return {
      type: 1,
      id: null,
      show: false,
      model: {
        content: "",
        eventType: "",
      },
      rules: {
        content: [
          {
            required: true,
            message: "请输入意见",
          },
        ],
      },
      // 遮罩层
      loading: true,
      titleList: [],
      info: {
        budgetDeclareVos: [],
      },
    };
  },
  mounted() {
    this.id = this.$route.query.id;
    this.type = this.$route.query.type;
    this.getInfo();
  },
  methods: {
    /** 审批 */
    handleOpinion(type) {
      this.show = true;
      this.model.eventType = type;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let txt = this.model.eventType == 4 ? "通过" : "退回";
          this.$confirm(`确认是否${txt}?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              const data = {
                ...this.model,
                id: this.id,
              };
              auditSummary(data).then((res) => {
                if (res.code === 700) {
                  this.$message.warning(res.msg);
                  return false;
                }
                this.$message.success("操作成功");
                this.goBack();
              });
            })
            .catch(() => {});
        }
      });
    },
    close() {
      this.resetForm("form");
      this.show = false;
    },
    async getInfo() {
      const that = this;
      await detailDynamicTitleTemplate(that.id).then((res) => {
        if (res.code === 700) {
          that.$message.warning(res.msg);
          return false;
        }
        let arr = res.data;
        detail(that.id).then((info) => {
          if (info.code === 700) {
            that.$message.warning(info.msg);
            return false;
          }
          if (info.data) {
            that.info = { ...that.info, ...info.data };
            that.info.budgetDeclareVos = that.info.budgetDeclareVos.map(
              (item, index) => {
                item.typeConfigItemRecordValues.map((it, i) => {
                  // debugger;
                  arr.map((a, s) => {
                    a[`val${index}`] = item.typeConfigItemRecordValues.splice(
                      0,
                      a.subTitleNames.length
                    );
                  });
                  // item["a" + s] = it;
                  return it;
                });
                return item;
              }
            );
            that.titleList = arr;
          } else {
            that.info = {
              budgetDeclareVos: [],
            };
          }
          // that.info.budgetDeclareVos = that.info.budgetDeclareVos.map(
          //   (item) => {
          //     item.typeConfigItemRecordValues.map((it, i) => {
          //       item["a" + i] = it;
          //       return it;
          //     });
          //     return item;
          //   }
          // );
          // that.titleList = arr;
        });
      });
    },
    submit(type) {
      this.$confirm("确认是否上报台账?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = { id: this.id, eventType: type };
          auditSummary(data).then((res) => {
            if (res.code === 700) {
              this.$message.warning(res.msg);
              return false;
            }
            this.$message.success("操作成功");
            this.goBack();
          });
        })
        .catch(() => {});
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
::v-deep
  .el-descriptions--medium:not(.is-bordered)
  .el-descriptions-item__cell {
  padding-bottom: 0 !important;
}
.red {
  color: #ed5565;
}
.green {
  color: #10b767;
}
</style>
